package com.lazi.june.web.encrypt;

import com.lazi.june.web.WebProperties;
import jakarta.servlet.DispatcherType;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

/**
 * desc:  ApiDecryptAutoConfiguration
 *
 * @author huliqian
 * @since 2025/9/12
 */
@AutoConfiguration
@EnableConfigurationProperties(WebProperties.class)
@ConditionalOnProperty(prefix = WebProperties.PREFIX, value = "api-decrypt.enabled", havingValue = "true")
public class ApiDecryptAutoConfig {


    @Bean
    public FilterRegistrationBean<CryptoFilter> cryptoFilterRegistration(WebProperties webProperties,
                                                                         RequestMappingHandlerMapping requestMappingHandlerMapping,
                                                                         HandlerExceptionResolver handlerExceptionResolver) {
        FilterRegistrationBean<CryptoFilter> registration = new FilterRegistrationBean<>();
        registration.setDispatcherTypes(DispatcherType.REQUEST);
        registration.setFilter(new CryptoFilter(webProperties, requestMappingHandlerMapping, handlerExceptionResolver));
        registration.addUrlPatterns("/*");
        registration.setName("cryptoFilter");
        registration.setOrder(FilterRegistrationBean.HIGHEST_PRECEDENCE);
        return registration;
    }

}
